const app = getApp()
import { currentDates, time, timeBefter, secondss } from '../../utils/util'
import Pay from '../../utils/pay'
Page({
  data: { imgurl: app.globalData.imgurl, show: false },
  onLoad({ o_id, s_id }) {
    this._getData(o_id)
    this.setData({ o_id, s_id })
  },
  async _getData(o_id) {
    let { code, data } = await wx.requestAsync({
      url: '/order/detail',
      data: {
        o_id,
      },
    })
    if (code == 0) {
      data.order.create_at =
        currentDates(data.order.create_at * 1000, 1) +
        ' ' +
        secondss(data.order.create_at)
      if (data.spell.begin_at) {
        let date = (data.spell.begin_at =
          currentDates(data.spell.begin_at * 1000) +
          ' ' +
          time(data.spell.begin_at) +
          '-' +
          time(data.spell.end_at))
        this.setData({
          date,
        })
      } else {
        let date =
          currentDates(data.parent.pre_at * 1000) +
          ' ' +
          time(data.parent.pre_at) +
          '-' +
          timeBefter(data.parent.pre_at, data.parent.hour)
        this.setData({
          date,
        })
      }
      this.setData(data)
    }
  },
  // 去使用
  _toCode() {
    const { o_id } = this.data
    wx.navigateTo({
      url: '/pages/code/code?o_id=' + o_id,
    })
  },
  // 取消订单
  async _toCancel() {
    const { o_id } = this.data
    let { code } = await wx.requestAsync({
      url: '/order/cancel',
      data: {
        o_id,
      },
    })
    if (code == 0) {
      return wx.showToast({
        title: '取消订单成功',
        icon: 'none',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/appointment/appointment',
            })
          }, 1000)
        },
      })
    }
  },
  // 点击付款
  async _toCash() {
    let { code, data } = await wx.requestAsync({
      url: '/order/token',
    })
    if (code == 0) {
      this.setData({
        token: data,
        show: true,
      })
    }
  },
  // 选择支付方式
  _success({ detail }) {
    this.setData({
      pay_method: detail,
      show: false,
    })
    this._pay()
  },
  // 支付
  async _pay() {
    let { o_id, token, pay_method, active } = this.data
    let { code, data } = await wx.requestAsync({
      url: '/order/pay',
      data: { o_id, token, pay_method },
    })
    if (code == 0) {
      if (!data) {
        wx.showToast({
          title: '支付成功',
          icon: 'none',
          duration: 2000,
          success: () => {
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/appointment/appointment',
              })
            }, 1000)
          },
        })
      } else {
        // 调用微信支付接口
        Pay({ ...data }, data)
      }
    }
  },
  // 去退款
  _torefund() {
    const { o_id } = this.data
    wx.navigateTo({
      url: '/pages/refund/refund?o_id=' + o_id,
    })
  },
  // 去评价
  _toevaluation() {
    const { o_id } = this.data
    wx.navigateTo({
      url: '/pages/evaluation/evaluation?o_id=' + o_id,
    })
  },
  // 去往门店
  _toStore() {
    let { s_id } = this.data
    wx.navigateTo({
      url: '/pages/storeIntroduction/storeIntroduction?s_id=' + s_id,
    })
  },
})
